//
//  ByUIChangeNickNameVC.m
//  itboye_shop_ios
//
//  Created by justiOS on 17/2/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIChangeNickNameVC.h"

// view
#import "ByViewNickNameTF.h"
// data
#import "ByApiUser.h"
@interface ByUIChangeNickNameVC ()
{
    ByViewNickNameTF *_tf;
}
@end

@implementation ByUIChangeNickNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBYItemviews:@[@"保存"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self addLeftBackBarButtonItem];
    self.title = @"修改昵称";
    [self createTextField];
    
    self.view.backgroundColor = TABLE_COLOR;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ByModelUser *user = [ByCacheUser getUserInfo];
    if (user.nickname.length > 0) {
        _tf.text = user.nickname;
    }
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_tf resignFirstResponder];
}
- (void)createTextField{
    
    _tf = [[ByViewNickNameTF alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 30) placeholder:@"请输入您的昵称"];
    if (BY_USER.nickname.length > 0) {
        _tf.text = BY_USER.nickname;
    }
    [_tf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    _tf.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tf];
    [_tf becomeFirstResponder];
}
//保存更新昵称
- (void)rightItemsAction:(NSInteger)rightIndex {
    if (_tf.text.length > 0) {
        if (_tf.text.length > 16) {
            [ByCoreToolAlert showFailureWithText:@"昵称长度不能超过16位"];
            return;
        }
        ByModelUser *user = [ByCacheUser getUserInfo];
        ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
        model.uid = user.uid;
        model.s_id = user.s_id;
        model.nickname = _tf.text;
        [ByApiUser updateUserInfo:model success:^(ByNetBaseResult *result) {
            user.nickname = _tf.text;
            [ByCacheUser setUserInfo:user];
            [ByCoreToolAlert showSuccessWithText:result.msg];
            [self dismissViewControllerAnimated:YES completion:nil];
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        [ByCoreToolAlert showOnlyText:@"请输入昵称"];
    }
    
}


- (void)textFieldDidChange:(UITextField *)textField{
    if (textField == _tf) {
        if (textField.text.length > 16) {
            textField.text = [textField.text substringToIndex:16];
            [ByCoreToolAlert showOnlyText:@"昵称最长16个字哦"];
        }
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
